package org.example.sdpback.sdk.listenners;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.example.sdpback.domain.entity.TestCase;
import org.example.sdpback.domain.entity.User;
import org.example.sdpback.domain.enums.TestCaseLevelEnum;
import org.example.sdpback.domain.enums.TestCaseStatusEnum;
import org.example.sdpback.domain.enums.TestCaseTypeEnum;
import org.example.sdpback.domain.po.TestCaseFilePO;
import org.example.sdpback.service.TestCaseService;
import org.example.sdpback.utils.ThreadLocalUtil;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class TestCaseDataListener extends AnalysisEventListener<TestCaseFilePO> {
    // 批处理大小
    private static final int BATCH_COUNT = 100;
    private List<TestCaseFilePO> dataList = new ArrayList<>();
    private List<TestCase>  testCaseList = new ArrayList<>();
    
    // 假设有一个数据处理服务
    private TestCaseService dataService;
    
    public TestCaseDataListener(TestCaseService dataService) {
        this.dataService = dataService;
    }
    
    /**
     * 每一条数据解析都会调用此方法
     */
    @Override
    public void invoke(TestCaseFilePO data, AnalysisContext context) {
        dataList.add(data);
//        System.out.println("当前行数：" + context.readRowHolder().getRowIndex());
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (dataList.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理list
            dataList.clear();
        }
    }
    
    /**
     * 所有数据解析完成会调用此方法
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 这里也要保存数据，确保最后遗留的数据也存储到数据库
        saveData();
    }
    
    /**
     * 加上存储数据库
     */
    private void saveData() {
        // 获取当前用户
        User user = ThreadLocalUtil.get();

        dataList.forEach(testCaseFilePO -> {
            String name = testCaseFilePO.getName();
            String level = testCaseFilePO.getLevel();
            String type = testCaseFilePO.getType();
            String apiPath = testCaseFilePO.getApiPath();
            String result = testCaseFilePO.getResult();
            String status = testCaseFilePO.getStatus();
            String tag = testCaseFilePO.getTag();

            TestCase testCase = new TestCase();
            testCase.setName(name);
            Arrays.stream(TestCaseLevelEnum.values()).filter(testCaseLevelEnum -> testCaseLevelEnum.getName().equals(level)).findFirst().ifPresent(testCase::setLevel);
            Arrays.stream(TestCaseTypeEnum.values()).filter(testCaseTypeEnum -> testCaseTypeEnum.getName().equals(type)).findFirst().ifPresent(testCase::setType);
            testCase.setApiPath(apiPath);
            testCase.setResult(result);
            Arrays.stream(TestCaseStatusEnum.values()).filter(testCaseStatusEnum -> testCaseStatusEnum.getName().equals(status)).findFirst().ifPresent(testCase::setStatus);
            testCase.setTag(tag);
            testCase.setCreatorId(String.valueOf(user.getId()));
            testCase.setCreateTime(LocalDateTime.now());
            testCase.setUpdateTime(LocalDateTime.now());

            testCaseList.add(testCase);
        });
        dataService.saveBatch(testCaseList);
    }
}
